﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Entities;

namespace QLTB_CTK34
{
    public partial class FormThongTinChiTiet : Form
    {
        private string MaMT;
        private Button btn;
        private IEnumerable<ThietBi> tb;
        private IEnumerable<TaiKhoan> tk;
        private IEnumerable<PhanCung> pc;

        public FormThongTinChiTiet()
        {
            InitializeComponent();
            drThongTinTK.AutoGenerateColumns = false;
            this.MaximizeBox = false;
        }
        public FormThongTinChiTiet(Button _mamt):this()
        {
            btn = _mamt;
            MaMT = _mamt.Text;
        }
        /// <summary>
        /// load form thông tin chi Tiết.
        /// </summary>
        private void FormThongTinChiTiet_Load(object sender, EventArgs e)
        {
            drThongTinTK.Visible = false;
            LoadThongTin();
        }
        /// <summary>
        /// Load Thông Tin Chi tiết máy tính, tài khoản,danh sách phần cứng.
        /// </summary>
        private void LoadThongTin()
        {
            
            tb = QLThietBi.FindByCondition(x => x.MayTinhs.Count > 0 && x.MayTinhs[0].MaMT == MaMT);
            tk = QLTaiKhoan.FindByCondition(x => x.MayDangNhap == MaMT && x.TrangThai.ToLower() == "online");
            pc = QLPhanCung.FindByCondition(x => x.MaMT == MaMT);
            txtTrangThai.Text = "offline";
            if (tk.Count() > 0)
            {
                txtMaTK.Text = tk.First().TenDangNhap;
                txtTrangThai.Text = tk.First().TrangThai;
                btnXemTT.Enabled = true;
            }
            txtMaMay.Text = MaMT;
            pbHinh.Image = btn.BackgroundImage;
            if (tb.Count() > 0)
            {
                txtMaTB.Text = tb.First().MaTB;
                txtTenTB.Text = tb.First().LoaiTB;
                txtXuatXu.Text = tb.First().XuatXu;
                txtNhanHieu.Text = tb.First().NhanHieu;
                dtpNgayNhap.Value = tb.First().NgayNhap;
                if (tb.First().NgayLapRap != null)
                    dtpNgayLapRap.Text = tb.First().NgayLapRap.Value.ToShortDateString();
                else
                    dtpNgayLapRap.Enabled = false;
                txtTinhTrang.Text = tb.First().TinhTrang;
            }

            foreach (ListViewItem x in lvPhanCung.Items)
            {
                if (pc.ToList().Where(i => i.TenPhanCung.ToLower() == x.SubItems[0].Text.ToLower()).Count() > 0)
                    x.Checked = true;
            }
        }
        /// <summary>
        /// click vao button thoát form đóng lại.
        /// </summary>
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// click vao button xem thông tin thông tin tài khoản sẽ hiển thị.
        /// </summary>
        private int click = 0;
        private void btnXemTT_Click(object sender, EventArgs e)
        {
            if (click == 0)
            {
                this.Height += 100;
                drThongTinTK.Visible = true;
                drThongTinTK.DataSource = QLTaiKhoan.FindByCondition(x => x.MaTK == tk.First().MaTK).ToList();
                click++;
            }
            else if (click == 1)
            {
                this.drThongTinTK.Visible = false;
                this.Height -= 100;
                click--;
            }
        }
    }
}
